package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.main.adapter.ChannelAdapter;
import com.tianyang.yukuaifu.business.main.adapter.RateConfigsAdapter;
import com.tianyang.yukuaifu.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifu.business.main.bean.PosRateOutput;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.business.main.contract.RateConfigContract;
import com.tianyang.yukuaifu.business.main.presenter.RateConfigPresenter;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description：费率配置
 * Created by xyj on 2024/3/26
 */
public class RateConfigsActivity extends BaseEditActivity implements RateConfigContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.channelRv)
    RecyclerView channelRv;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private RelativeLayout emptyView;

    private Button confirmTv;

    private RateConfigsAdapter vRateConfigsAdapter;

    private ChannelAdapter vChannelAdapter;

    private RateConfigContract.Presenter mPresenter;

    private String posChannel = "ys", params = "";

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        boolean isRateConfig = getIntent().getBooleanExtra(MainConfig.INTENT_IS_RATE_CONFIG, false);
        int isAgent = getIntent().getIntExtra(MainConfig.INTENT_IS_AGENT, 0);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vRateConfigsAdapter = new RateConfigsAdapter(isRateConfig);
        vChannelAdapter = new ChannelAdapter();
        channelRv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        channelRv.setAdapter(vChannelAdapter);
        View footerView = View.inflate(mContext, R.layout.layout_store_manage_footer, null);
        emptyView = footerView.findViewById(R.id.emptyView);
        confirmTv = footerView.findViewById(R.id.confirmTv);
        confirmTv.setText("确认");
        vRateConfigsAdapter.addFooterView(footerView);
        vRecyclerview.setAdapter(vRateConfigsAdapter);

        if (!isRateConfig) {
            commonTitle.setTitle(isAgent == 0 ? "商户费率" : "代理商费率");
            confirmTv.setVisibility(View.GONE);
        } else {
            commonTitle.setTitle("费率配置");
            confirmTv.setVisibility(View.VISIBLE);
        }
        if (getIntent().hasExtra(MainConfig.INTENT_POS_CHANNEL)) {
            posChannel = getIntent().getStringExtra(MainConfig.INTENT_POS_CHANNEL);
        }
        if (getIntent().hasExtra(MainConfig.INTENT_PARAMS)) {
            params = getIntent().getStringExtra(MainConfig.INTENT_PARAMS);
        }
        String userName = getIntent().getStringExtra(MainConfig.INTENT_USER_NAME);
        mPresenter = new RateConfigPresenter(this, userName, isAgent, params);
        if (isAgent == 1) {
            mPresenter.getChannelList();
        } else {
            mPresenter.getPosTemplateRate(posChannel);
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getPosTemplateRate(posChannel);
            }
        });
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRateData(vRateConfigsAdapter.getData());
            }
        });
        vChannelAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ChannelOutput output = vChannelAdapter.getItem(position);
                if (output != null) {
                    vChannelAdapter.setChooseIndex(position);
                    mPresenter.getPosTemplateRate(output.getCode());
                }
            }
        });
    }

    @Override
    public void showChannelView(List<ChannelOutput> outputs) {
        if (outputs != null && !outputs.isEmpty()) {
            vChannelAdapter.setNewData(outputs);
        }
    }

    @Override
    public void showPosTemplateRateView(List<PosRateOutput> outputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (outputs != null && outputs.size() > 0) {
            vRateConfigsAdapter.setNewData(outputs);
            emptyView.setVisibility(View.GONE);
        } else {
            vRateConfigsAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    private void setRateData(List<PosRateOutput> mRateOutputs) {
        if (mRateOutputs != null && mRateOutputs.size() > 0) {
            for (int position = 0; position < mRateOutputs.size(); position++) {
                PosRateOutput output = mRateOutputs.get(position);
                if (output != null) {
                    if (TextUtils.isEmpty(output.getChannelRate())) {
                        ToastUtil.show("请输入" + output.getProductName() + "费率");
                        return;
                    }
                    if (!VerifyUtil.isDifferenceNum(output.getChannelRate(), output.getChannelRateMin(), output.getChannelRateMax())) {
                        ToastUtil.show(output.getProductName() + "费率超出范围");
                        return;
                    }
                    if (TextUtils.isEmpty(output.getChannelSettlement())) {
                        ToastUtil.show("请输入" + output.getProductName() + "笔数费");
                        return;
                    }
                    if (!VerifyUtil.isDifferenceNum(output.getChannelSettlement(), output.getChannelSettlementMin(), output.getChannelSettlementMax())) {
                        ToastUtil.show(output.getProductName() + "笔数费超出范围");
                        return;
                    }
                    mRateOutputs.get(position).setChannel(output.getProductName());
                }
            }
        }
        mPresenter.addPosUserRate(mRateOutputs);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(RateConfigContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
